package com.wmwj.hxlj.mapper;

import com.wmwj.hxlj.entity.History;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HistoryMapper {

    /**
     * 插入历史记录
     */
    @Insert("INSERT INTO history (user_id, content_id)" +
            " VALUES (#{userId}, #{contentId})")
    int insert(History history);

    /**
     * 根据用户 ID 查询历史记录
     */
    @Select("SELECT * FROM history WHERE user_id = #{userId} ORDER BY timestamp DESC LIMIT #{pageStarts}, #{pageSize}")
    List<History> selectByUserId(Long userId, int pageStarts, int pageSize);

    /**
     * 根据 ID 删除历史记录
     */
    @Delete("DELETE FROM history WHERE id = #{id}")
    int deleteById(Long id);

    /**
     * 根据用户 ID 统计历史记录数量
     */
    @Select("SELECT COUNT(*) FROM history WHERE user_id = #{userId}")
    int countByUserId(Long userId);

    /**
     * 根据 ID 查询历史记录，用于查询上一条历史记录
     */
    @Select("SELECT * FROM history WHERE  content_id = #{contentId} LIMIT 0,1")
    History findPreById(Long contentId);

    /**
     * 根据 ID 查询历史记录
     * @param id
     */
    @Select("SELECT * FROM history WHERE  id = #{id}")
    History findById(Long id);

}